using UnityEngine;

public class SceneGame : MonoBehaviour
{
	
	public static SceneGame Instance;
	
	public  int       Step;
	public  Texture2D BlackLayer;
	public  float     FadeInSpeed;
	public  float     FadeOutSpeed;
	private string    _NextLevel;
	private float     _Alpha       = 1.0f;
	
	public void Start()
	{
		Instance = this;
	}
	
	public void Update()
	{
		float smoothDeltaTime = Mathf.Clamp(Time.smoothDeltaTime, 0.01666667f, float.MaxValue);
		switch (Step)
		{
		case 0:
			_Alpha = Mathf.Clamp(_Alpha - FadeInSpeed * smoothDeltaTime, 0.0f, 1.0f);
			if (_Alpha == 0.0f)
				++Step;
			break;
		case 1:
			if (Input.GetKeyDown(KeyCode.Escape))
			{
				_NextLevel = "SceneTitle";
				++Step;
			}
			break;
		case 2:
			_Alpha       = Mathf.Clamp(_Alpha + FadeOutSpeed * smoothDeltaTime, 0.0f, 1.0f);
			audio.volume = 0.75f * (1.0f - _Alpha);
			if (_Alpha == 1.0f)
				++Step;
			break;
		case 3:
			if (string.IsNullOrEmpty(_NextLevel))
				Application.LoadLevel("SceneGameOver");
			else
				Application.LoadLevel(_NextLevel);
			break;
		}
	}
	
	public void OnGUI()
	{
		if (_Alpha == 0.0f)
			return;
		Color tempColor = GUI.color;
		GUI.color       = Color.white * _Alpha;
		GUI.DrawTexture(
			new Rect(0.0f, 0.0f, Screen.width, Screen.height),
			BlackLayer
		);
		GUI.color = tempColor;
	}
	
}